<!-- syscontrolmgr系统控制 -->
<template>
  <a-card :bordered="false" style="margin: 6px">
    <m-table
      ref="tableRef"
      :columns="columns"
      :data="loadData"
      :alert="options.alert.show"
      bordered
      :row-key="(record) => record.ID"
      :tool-config="toolConfig"
      :row-selection="options.rowSelection"
      :show-pagination="true"
      :scroll="{ x: 'max-content' }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'CtrlValue'">
          <!-- 开关展示区域 -->
          <div
            v-if="
              record.CtrlKey === '翻卡灯控关闭' ||
              record.CtrlKey === '灯控关闭' ||
              record.CtrlKey === '通过率控制启用'
            "
          >
            <a-switch
              :checked="record.CtrlValue == 1"
              @click="changeEnable($event, record)"
              :disabled="!hasEditRole(route.meta.RolePower)"
            ></a-switch>
          </div>
          <!-- 以下不做开关展示 -->
          <div
            v-else
            style="display: flex; align-items: center; justify-content: center"
          >
            <div v-if="record.CtrlKey === '老化通过率'">
              <a-input
                size="small"
                type="number"
                :value="agePassRate"
                style="width: 80px; margin-right: 10px; text-align: center"
                :readonly="passRate != 1"
                max="1"
                min="0.01"
                step="0.01"
                :maxlength="3"
                @input="changeAgePassRateValue($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="passRate == 1 && hasEditRole(route.meta.RolePower)"
                @click="agePassRateSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '检测通过率'">
              <a-input
                size="small"
                type="number"
                :value="checkPassRate"
                style="width: 80px; margin-right: 10px; text-align: center"
                :readonly="passRate != 1"
                max="1"
                min="0.01"
                step="0.01"
                @input="changeCheckPassRate($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="passRate == 1 && hasEditRole(route.meta.RolePower)"
                @click="checkPassRateSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '一次通过率'">
              <a-input
                size="small"
                type="number"
                :value="onePassRate"
                style="width: 80px; margin-right: 10px; text-align: center"
                :readonly="passRate != 1"
                max="1"
                min="0.01"
                step="0.01"
                @input="changeOnePassRate($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="passRate == 1 && hasEditRole(route.meta.RolePower)"
                @click="onePassRateSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '翻卡时间'">
              <a-input
                size="small"
                :value="fankaTime"
                style="width: 80px; margin-right: 10px; text-align: center"
                type="number"
                min="1"
                step="1"
                @input="changeFankaTime($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="hasEditRole(route.meta.RolePower)"
                @click="fankaTimeSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '翻卡开始标准'">
              <a-input
                size="small"
                :value="fanka"
                style="width: 80px; margin-right: 10px; text-align: center"
                type="number"
                max="1"
                min="0.1"
                step="0.1"
                @input="changeFanka($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="hasEditRole(route.meta.RolePower)"
                @click="fankaSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '仪表电流下限'">
              <a-input
                size="small"
                :value="currentLow"
                style="width: 80px; margin-right: 10px; text-align: center"
                type="number"
                min="0"
                step="1"
                @input="changeCurrentLow($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="hasEditRole(route.meta.RolePower)"
                @click="currentLowSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '仪表电流上限'">
              <a-input
                size="small"
                :value="currentUpper"
                style="width: 80px; margin-right: 10px; text-align: center"
                type="number"
                step="1"
                @input="changeCurrentUpper($event)"
              />
              <a-button
                size="small"
                type="primary"
                @click="currentUpperSubmit(record.CtrlKey)"
                v-show="hasEditRole(route.meta.RolePower)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '仪表电压下限'">
              <a-input
                size="small"
                :value="voltageLow"
                style="width: 80px; margin-right: 10px; text-align: center"
                type="number"
                min="0"
                step="1"
                @input="changeVoltageLow($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="hasEditRole(route.meta.RolePower)"
                @click="voltageLowSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
            <div v-if="record.CtrlKey === '仪表电压上限'">
              <a-input
                size="small"
                :value="voltageUpper"
                style="width: 80px; margin-right: 10px; text-align: center"
                type="number"
                step="1"
                @input="changeVoltageUpper($event)"
              />
              <a-button
                size="small"
                type="primary"
                v-show="hasEditRole(route.meta.RolePower)"
                @click="voltageUpperSubmit(record.CtrlKey)"
                >修改</a-button
              >
            </div>
          </div>
        </template>
      </template>
    </m-table>
  </a-card>

  <!-- -->
  <!--  <Form ref="formRef" @successful="tableRef.refresh(true)" /> -->
</template>
      
  <script setup name="deptmgr">
/* import Form from "./form.vue"; */
import { ref, computed } from "vue";
import { message } from "ant-design-vue";
import syscontrolmgrApi from "@/api/systemManagement/syscontrolmgrApi";
import { transitionTime } from "@/utils/time.js";
import { useRoute } from "vue-router";
const route = useRoute();
const searchFormState = ref({
  ID: 0,
  CtrlKey: "",
});
/* const formRef = ref(); */

const tableRef = ref();
const toolConfig = {
  refresh: true,
  height: true,
  columnSetting: false,
  striped: false,
};
const columns = [
  {
    title: "控制名称",
    dataIndex: "CtrlKey",
    align: "center",
  },
  {
    title: "控制值",
    dataIndex: "CtrlValue",
    align: "center",
    width: 200,
  },
  {
    title: "控制说明",
    dataIndex: "CtrlDesp",
    align: "center",
  },

  /*  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    width: "200px",
  }, */
];
// 列表选择配置
const options = {
  alert: {
    show: false,
    clear: () => {
      selectedRowKeys = ref([]);
    },
  },
};
const searchFormRef = ref();
const reset = () => {
  searchFormRef.value.resetFields();
  tableRef.value.refresh(true);
};
// 通过控制率开关是否启用
let passRate = ref();
// 老化通过率
let agePassRate = ref();
// 检测通过率
let checkPassRate = ref();
// 一次通过率
let onePassRate = ref();
// 翻卡时间
let fankaTime = ref("");
// 翻卡开始标准
let fanka = ref("");
// 仪表电流下限
let currentLow = ref("");
let _currentLow = ref("");
// 仪表电流上限
let currentUpper = ref("");
// 仪表电压下限
let voltageLow = ref("");
let _voltageLow = ref("");
// 仪表电压上限
let voltageUpper = ref("");
let data = ref([]);
const loadData = (parameter) => {
  return syscontrolmgrApi
    .getSysControls(Object.assign(parameter, searchFormState.value))
    .then((res) => {
      data.value = res.Data;
      data.value.forEach((item) => {
        if (item.CtrlKey === "通过率控制启用") passRate.value = item.CtrlValue;
        if (item.CtrlKey === "老化通过率") agePassRate.value = item.CtrlValue;
        if (item.CtrlKey === "检测通过率") checkPassRate.value = item.CtrlValue;
        if (item.CtrlKey === "一次通过率") onePassRate.value = item.CtrlValue;
        if (item.CtrlKey === "翻卡时间") fankaTime.value = item.CtrlValue;
        if (item.CtrlKey === "翻卡开始标准") fanka.value = item.CtrlValue;
        if (item.CtrlKey === "仪表电流下限") {
          currentLow.value = item.CtrlValue;
          _currentLow.value = item.CtrlValue;
        }
        if (item.CtrlKey === "仪表电流上限")
          currentUpper.value = item.CtrlValue;
        if (item.CtrlKey === "仪表电压下限") {
          voltageLow.value = item.CtrlValue;
          _voltageLow.value = item.CtrlValue;
        }
        if (item.CtrlKey === "仪表电压上限")
          voltageUpper.value = item.CtrlValue;
      });

      return res;
    });
};

const changeEnable = (bool, record) => {
  let code = bool ? 1 : 0;
  syscontrolmgrApi
    .modifySysControl({
      CtrlKey: record.CtrlKey,
      CtrlValue: code,
    })
    .then((res) => {
      message.success("修改成功");
      tableRef.value.refresh();
    });
};
// 改变老化通过率的值

const changeAgePassRateValue = (e) => {
  if (e.target.value) {
    e.target.value = parseFloat(e.target.value.replace(/[a-zA-Z]/gi, ""));
  } else {
    e.target.value = 0;
  }
  if (e.target.value.length > 4) {
    e.target.value = e.target.value.slice(0, 4);
  }
  if (e.target.value > 1) {
    e.target.value = 1;

    message.error("老化通过率不能大于1");
  }
  if (e.target.value < 0.1) {
    message.error("老化通过率不能小于0.1");
  }
  agePassRate.value = e.target.value;
};
// 改变检测通过率的值
const agePassRateSubmit = (CtrlKey) => {
  if (agePassRate.value < 0.1) {
    message.error("老化通过率不能小于0.1");
    tableRef.value.refresh();
    return;
  }
  if (agePassRate.value > 1) {
    message.error("老化通过率不能大于1");
    tableRef.value.refresh();
    return;
  }
  modifySystem(CtrlKey, agePassRate.value);
};
// 检测合格率修改

const changeCheckPassRate = (e) => {
  if (e.target.value) {
    e.target.value = parseFloat(e.target.value.replace(/[a-zA-Z]/gi, ""));
  } else {
    e.target.value = 0;
  }
  if (e.target.value.length > 4) {
    e.target.value = e.target.value.slice(0, 4);
  }
  if (e.target.value > 1) {
    e.target.value = 1;

    message.error("检测通过率不能大于1");
  }
  if (e.target.value < 0.1) {
    message.error("检测通过率不能小于0.1");
  }
  checkPassRate.value = e.target.value;
};
// 改变检测通过率的值
const checkPassRateSubmit = (CtrlKey) => {
  if (checkPassRate.value < 0.1) {
    message.error("检测通过率不能小于0.1");
    tableRef.value.refresh();

    return;
  }
  if (checkPassRate.value > 1) {
    message.error("检测通过率不能大于1");
    tableRef.value.refresh();

    return;
  }
  modifySystem(CtrlKey, checkPassRate.value);
};
// 一次通过率修改

const changeOnePassRate = (e) => {
  if (e.target.value) {
    e.target.value = parseFloat(e.target.value.replace(/[a-zA-Z]/gi, ""));
  } else {
    e.target.value = 0;
  }
  if (e.target.value.length > 4) {
    e.target.value = e.target.value.slice(0, 4);
  }
  if (e.target.value > 1) {
    e.target.value = 1;

    message.error("检测通过率不能大于1");
  }
  if (e.target.value < 0.1) {
    message.error("检测通过率不能小于0.1");
  }
  onePassRate.value = e.target.value;
};
// 改变检测通过率的值
const onePassRateSubmit = (CtrlKey) => {
  if (onePassRate.value < 0.1) {
    message.error("检测通过率不能小于0.1");
    tableRef.value.refresh();

    return;
  }
  if (onePassRate.value > 1) {
    message.error("检测通过率不能大于1");
    tableRef.value.refresh();

    return;
  }
  modifySystem(CtrlKey, onePassRate.value);
};
// 改变翻卡时间
const changeFankaTime = (e) => {
  fankaTime.value = e.target.value;
  if (e.target.value) {
    e.target.value = parseInt(e.target.value.replace(/[a-zA-Z]/gi, ""));
  } else {
    e.target.value = 0;
  }
  if (e.target.value < 1) {
    message.error("翻卡时间不能小于1");
  }

  console.log(fankaTime.value, "changeFankaTime");
};
const fankaTimeSubmit = (CtrlKey) => {
  if (fankaTime.value < 1) {
    message.error("翻卡时间不能小于1");
    tableRef.value.refresh();
    return;
  }
  modifySystem(CtrlKey, fankaTime.value);
};

// 翻卡开始标准

const changeFanka = (e) => {
  if (e.target.value) {
    e.target.value = parseFloat(e.target.value.replace(/[a-zA-Z]/gi, ""));
  } else {
    e.target.value = 0;
  }
  if (e.target.value.length > 3) {
    e.target.value = e.target.value.slice(0, 3);
  }
  if (e.target.value > 1) {
    e.target.value = 1;
    message.error("翻卡开始标准不能大于1");
  }
  if (e.target.value < 0.1) {
    message.error("翻卡开始标准不能小于0.1");
  }
  fanka.value = e.target.value;
};

const fankaSubmit = (CtrlKey) => {
  if (fanka.value < 0.1) {
    message.error("翻卡开始标准不能小于0.1");
    tableRef.value.refresh();

    return;
  }
  if (fanka.value > 1) {
    message.error("翻卡开始标准不能大于1");
    tableRef.value.refresh();
    return;
  }
  modifySystem(CtrlKey, fanka.value);
};

// 仪表电流下限部分--------------------
const changeCurrentLow = (e) => {
  if (e.target.value) {
    e.target.value = e.target.value.replace(/[a-zA-Z]/gi, "");
    e.target.value = parseFloat(e.target.value.replace(/(\.\d{2,})|\.$/));
  } else {
    e.target.value = 0;
  }

  if (e.target.value <= 0) {
    message.error("仪表电流下限必须大于0");
    e.target.value = 0;
  }
  currentLow.value = e.target.value;
};
const currentLowSubmit = (CtrlKey) => {
  if (currentLow.value <= 0) {
    message.error("仪表电流下限必须大于0");
    tableRef.value.refresh();
    return;
  }
  modifySystem(CtrlKey, currentLow.value);
};
// 修改仪表电流上限部分----------------------
const changeCurrentUpper = (e) => {
  if (e.target.value) {
    e.target.value = e.target.value.replace(/[a-zA-Z]/gi, "");
    e.target.value = parseFloat(e.target.value.replace(/(\.\d{2,})|\.$/));
  } else {
    e.target.value = 0;
  }
  // 如果存在下限
  console.log(parseFloat(e.target.value) <= parseFloat(_currentLow.value));
  if (_currentLow.value) {
    if (parseFloat(e.target.value) <= parseFloat(_currentLow.value)) {
      message.error("仪表电流上限必须大于仪表电流下限");
    }
  } else {
    // 不存在下限就要大于0
    if (e.target.value <= 0) {
      message.error("仪表电流上限必须大于0");
    }
  }
  currentUpper.value = e.target.value;
};
const currentUpperSubmit = (CtrlKey) => {
  // 如果存在下限
  if (_currentLow.value) {
    if (currentUpper.value <= parseFloat(_currentLow.value)) {
      message.error("仪表电流上限必须大于仪表电流下限");
      return;
    }
  } else {
    // 不存在下限就要大于0
    if (currentUpper.value <= 0) {
      message.error("仪表电压上限必须大于0");
      return;
    }
  }
  modifySystem(CtrlKey, currentUpper.value);
};
// 修改仪表电压下限部分
const changeVoltageLow = (e) => {
  if (e.target.value) {
    e.target.value = e.target.value.replace(/[a-zA-Z]/gi, "");
    e.target.value = parseFloat(e.target.value.replace(/(\.\d{2,})|\.$/));
  } else {
    e.target.value = 0;
  }
  if (e.target.value <= 0) {
    message.error("仪表电压下限必须大于0");
    e.target.value = 0;
  }
  voltageLow.value = e.target.value;
};
const voltageLowSubmit = (CtrlKey) => {
  if (voltageLow.value <= 0) {
    message.error("仪表电压下限必须大于0");
    tableRef.value.refresh();
    return;
  }
  modifySystem(CtrlKey, voltageLow.value);
};
// 修改仪表电压上限部分--------

const changeVoltageUpper = (e) => {
  if (e.target.value) {
    e.target.value = e.target.value.replace(/[a-zA-Z]/gi, "");
    e.target.value = parseFloat(e.target.value.replace(/(\.\d{2,})|\.$/));
  } else {
    e.target.value = 0;
  }
  // 如果存在下限
  if (_voltageLow.value) {
    if (e.target.value <= _voltageLow.value) {
      message.error("仪表电压上限必须大于仪表电压下限");
    }
  } else {
    // 不存在下限就要大于0
    if (e.target.value <= 0) {
      message.error("仪表电压上限必须大于0");
    }
  }
  voltageUpper.value = e.target.value;
};
const voltageUpperSubmit = (CtrlKey) => {
  // 如果存在下限
  if (_voltageLow.value) {
    if (voltageUpper.value <= _voltageLow.value) {
      message.error("仪表电压上限必须大于仪表电压下限");
      return;
    }
  } else {
    // 不存在下限就要大于0
    if (voltageUpper.value <= 0) {
      message.error("仪表电压上限必须大于0");
      return;
    }
  }
  modifySystem(CtrlKey, voltageUpper.value);
};

// 调用修改--------------
const modifySystem = (CtrlKey, CtrlValue) => {
  syscontrolmgrApi
    .modifySysControl({
      CtrlKey,
      CtrlValue,
    })
    .then(() => {
      message.success("修改成功");
      tableRef.value.refresh();
    })
    .catch((error) => {
      console.log(error);
      tableRef.value.refresh();
    });
};
</script>
    <style lang="scss" scoped>
.deptmgr {
  width: 100%;
  height: 100%;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
  & > div {
    height: 100%;
    overflow: auto;
  }
  .deptmgr_tree {
    flex: 3;
    margin-right: 10px;
  }
  .deptmgr_content {
    flex: 8;
  }
}
</style>
      